package com.example.smartcity1.service.patient;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity1.R;
import com.example.smartcity1.bean.MsgCodeBean;
import com.example.smartcity1.bean.PatientOrderPostBean;
import com.example.smartcity1.network.RetrofitClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PatientClassPageActivity extends AppCompatActivity {

    private TextView patientNameText;
    private TextView categoryNameText;
    private TextView didText;
    private TextView moneyText;
    private EditText dateEdit;
    private EditText timeEdit;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_class_page);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle("预约挂号确认");
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        initView();
        
        Intent intent = getIntent();
        String patientName = intent.getStringExtra("patientName");
        String categoryName = intent.getStringExtra("categoryName");
        int divisionId = intent.getIntExtra("divisionId",1);
        int did = intent.getIntExtra("did",1);
        String money = intent.getStringExtra("money");

        patientNameText.setText(patientName);
        categoryNameText.setText(categoryName);
        if (did == 1) {
            didText.setText("专家门诊");
        } else if (did == 2){
            didText.setText("普通门诊");
        }
        moneyText.setText(money);

        SharedPreferences user = getSharedPreferences("user", MODE_PRIVATE);
        String token = user.getString("token", "");
        String userId = user.getString("userId", "1");
        
        button.setOnClickListener(v -> {
            if (dateEdit.getText().toString().equals("") || timeEdit.getText().toString().equals("")) {
                Toast.makeText(this, "请正确填写时间", Toast.LENGTH_SHORT).show();
            } else {
                PatientOrderPostBean bean = new PatientOrderPostBean();
                bean.setPatientName(patientName);
                bean.setDivisionId(divisionId);
                bean.setTypesId(String.valueOf(did));
                bean.setMoeny(money);
                bean.setUserId(userId);
                bean.setTypesId(dateEdit.getText().toString() + " " + timeEdit.getText().toString());
                RetrofitClient.serviceService().getPatientOrderData(token,bean).enqueue(new Callback<MsgCodeBean>() {
                    @Override
                    public void onResponse(Call<MsgCodeBean> call, Response<MsgCodeBean> response) {
                        MsgCodeBean body = response.body();
                        if (body != null && body.getCode() == 200) {
                            Intent intent1 = new Intent(PatientClassPageActivity.this,PatientClassYesActivity.class);
                            intent1.putExtra("patientName",patientName);
                            intent1.putExtra("categoryName",categoryName);
                            intent1.putExtra("did",did);
                            intent1.putExtra("money",money);
                            intent1.putExtra("date",dateEdit.getText().toString());
                            intent1.putExtra("time",timeEdit.getText().toString());
                            startActivity(intent1);
                            finish();
                        } else {
                            Toast.makeText(PatientClassPageActivity.this, "预约失败, 请重试", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<MsgCodeBean> call, Throwable t) {

                    }
                });
            }
        });
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }

    private void initView() {
        patientNameText = findViewById(R.id.patientName_text);
        categoryNameText = findViewById(R.id.categoryName_text);
        didText = findViewById(R.id.did_text);
        moneyText = findViewById(R.id.money_text);
        dateEdit = findViewById(R.id.date_edit);
        timeEdit = findViewById(R.id.time_edit);
        button = findViewById(R.id.button);
    }
}